package com.mebs.model.inventory;

import com.mebs.model.User;
import com.mebs.model.AttachedFile;

import javax.persistence.*;

import org.jt.jet.util.JetUtils;

import java.util.Set;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * Created by Sergey Prasolov, sap@jensentechnologies.com Date: Feb 20, 2006, 2:26:25 PM
 */
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "RO_ORDER")
@PrimaryKeyJoinColumn(name = "Id")
public class RelocationOrder extends AbstractOrder {
    @ManyToOne(optional = false)
    @JoinColumn(name = "FamilyId")
    private User family;

    @Column(name = "Sequence", nullable = false)
    private Integer sequence;

    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name = "OrderId")
    private Set<RelocationPiece> pieces;

    @ManyToOne(optional = true)
    @JoinColumn(name = "FileId")
    private AttachedFile file;

    @Column(name = "Allowances", nullable = true, length = 1024)
    private String allowances;


    /**
     * ********************** Getters-setters ***********************
     */
    public User getFamily()
    {
        return family;
    }

    public void setFamily(User family)
    {
        this.family = family;
    }

    public Set<RelocationPiece> getPieces()
    {
        return pieces;
    }

    public void setPieces(Set<RelocationPiece> pieces)
    {
        this.pieces = pieces;
    }

    public AttachedFile getFile()
    {
        return file;
    }

    public void setFile(AttachedFile file)
    {
        this.file = file;
    }

    public String getAllowances()
    {
        return allowances;
    }

    public void setAllowances(String allowances)
    {
        this.allowances = allowances;
    }

    public Integer getSequence()
    {
        return sequence;
    }

    public void setSequence(Integer sequence)
    {
        this.sequence = sequence;
    }

    @Override
    public String toString()
    {
        return getSequenceNumber();
    }

    @Transient
    public boolean isDeletable()
    {
        return JetUtils.isEmpty(this.pieces);
    }

    @Transient
    public String getSequenceNumber()
    {
        Calendar c = new GregorianCalendar();
        c.setTime(getDateOfOrder());
        return String.format("RO-%s-%05d-%02d%02d%02d",
            getCompany().getTicker() == null ? "XXX" : getCompany().getTicker(),
            getSequence(),
            c.get(Calendar.YEAR) % 100,
            c.get(Calendar.MONTH) + 1,
            c.get(Calendar.DAY_OF_MONTH));
    }

    @Transient
    public static Integer extractSequenceFromNumber(String number)
    {
        if (number == null) return null;

        String[] a = number.split("-");
        if (a.length < 3) return null;

        return Integer.valueOf(a[2]);
    }

    @Transient
    public static String extractTickerFromNumber(String number)
    {
        if (number == null) return null;

        String[] a = number.split("-");
        if (a.length < 2) return null;

        return a[1];
    }
}
